package com.cskaoyan.market.shiro;

import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.WxAuthService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author lixianbiao
 * @since 2024/07/01 15:07
 */
@Component
public class WxRealm extends AuthorizingRealm {
    @Autowired
    private WxAuthService wxAuthService;
    // 鉴权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    // 认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authenticationToken;
        String username = usernamePasswordToken.getUsername();
        MarketUser marketUser = wxAuthService.getMarketUser(username);
        String password = marketUser == null?"": marketUser.getPassword();
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(marketUser, password, getName());
        return authenticationInfo;
    }
}